Initializing the Instances of an Array of Some Class Type

You are probably aware that when you declare an array of some class type, you call the default constructor for that type as many times as you have elements in the array. Here we discuss how to put desired values into each element of an array since the default constructor puts in either garbage or default values.

It makes sense to create a separate function to handle this task. Let's call this function InitializeInstances'. The contract class has three properties and three "Change..." functions, one for each property. Therefore, for the zeroeth element of an array of Contracts one could write:

contracts[0].ChangeSquareFootage(1000);
contracts[0].ChangeNumDesks(3);
contracts[0].ChangeNumDays(5);

Now, suppose we have an array of five Contracts. We could write InitializeInstances as:

void InitializeInstances(ContractArray contracts)
{
contracts[0].ChangeSquareFootage(1000);
contracts[0].ChangeNumberOfDesks(3);
contracts[0].ChangeNumDays(5);

contracts[1].ChangeSquareFootage(500);
contracts[1].ChangeNumDesks(10);
contracts[1].ChangeNumDays(3);

contracts[2].ChangeSquareFootage(200);
contracts[2].ChangeNumDesks(1);
contracts[2].ChangeNumDays(4);

contracts[3].ChangeSquareFootage(2000);
contracts[3].ChangeNumDesks(20);
contracts[3].ChangeNumDays(5);

contracts[4].ChangeSquareFootage(300);
contracts[4].ChangeNumDesks(2);
contracts[4].ChangeNumDays(2);
}

The conclusion: If you have a class that defines a "Change..." function for each property you want to put a value into, you can write the code this way. However, there are at least two problems with this approach. One problem is that this code only handles an array of a specific size, five in this case. A second is the repetition involved. A third problem is that the data is hard coded. Let's deal with the third problem first. If we had three variables we could "hard code" their values as in:

    int value1 = 5;
    int value2 = 10;
    int value3 = 15;
However, in the programs we have written, we have been more likely to acquire each variable's value by asking a user to enter it through three
cout << "...";
cin >> ...
pairs as in:
    cout << "Please enter a value for variable 1";
    cin >> value1;
    cout << "Please enter a value for variable 2";
    cin >> value2;
    cout << "Please enter a value for variable 3";
    cin >> value3;
We can use this same approach with our "InitializeInstances" function. To handle the first array element we would write:
    cout << "Please enter the square footage";
    cin >> sqFootage;
    cout << "Please enter the number of desks ";
    cin >> numDesks;
    cout << "Please enter the number of days ";
    cin >> numDays;

    contracts[0].ChangeSquareFootage(sqFootage);
    contracts[0].ChangeNumDesks(numDesks);
    contracts[0].ChangeNumDays(numDays);

The other array elements, all contracts, would be handled in the same way and the code for the first three array elements would be:
    cout << "Please enter the square footage for contract 1";
    cin >> sqFootage;
    cout << "Please enter the number of desks for contract 1";
    cin >> numDesks;
    cout << "Please enter the number of days for contract 1";
    cin >> numDays;

    contracts[0].ChangeSquareFootage(sqFootage);
    contracts[0].ChangeNumDesks(numDesks);
    contracts[0].ChangeNumDays(numDays);

    cout << "Please enter the square footage for contract 2";
    cin >> sqFootage;
    cout << "Please enter the number of desks for contract 2";
    cin >> numDesks;
    cout << "Please enter the number of days for contract 2";
    cin >> numDays;

    contracts[1].ChangeSquareFootage(sqFootage);
    contracts[1].ChangeNumDesks(numDesks);
    contracts[1].ChangeNumDays(numDays);

    cout << "Please enter the square footage for contract 3";
    cin >> sqFootage;
    cout << "Please enter the number of desks for contract 3";
    cin >> numDesks;
    cout << "Please enter the number of days for contract 3";
    cin >> numDays;

    contracts[2].ChangeSquareFootage(sqFootage);
    contracts[2].ChangeNumDesks(numDesks);
    contracts[2].ChangeNumDays(numDays);

We now have taken care of problem 3, the hard coding. However, the code is still quite repetitious. Take a moment though and examine the code. Do you see a way it could be simplfied using a "for" loop?

***************************************

Hint 1: What stays the same and what changes?

Hint 2: Have you noticed that it is only the numbers that change AND that these numbers all go up by one for each new contract?

Take a moment to try and write a loop that only has one set of requests and one set of Change functions. When you are finished, compare what you wrote with this code.